﻿





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DrawableObjects.Actors;
using DrawableObjects.Actors.Weapons;
using DrawableObjects.Actors.Aircrafts;
using DrawableObjects.Actors.Aircrafts.Controller;
using DrawableObjects.Actors.Projectiles;
using GlobalGameClasses;

namespace Serialization
{
    /// <summary>
    /// A class checking for game events. Once specific events have or haven't been thrown an achievement will be unlocked permanently for
    /// the user's account.
    /// </summary>
    public class AchievementHandler
    {
        private float numberOfEnemies;
        private float enemiesKilled;
        private TimeSpan start;
        private bool damage;
        private bool damageBoss;
        private bool bossActive;
        private int weaponsystemsUpgraded;
        private float regeneratedHp;
        private int collectedItems;

	    public void OnDestroy(Actor sender, Actor destroyedBy)
	    {
            if ((((Aircraft)sender).controller is AIControlledAircraft_PathGuided || ((Aircraft)sender).controller is AIControlledAircraft_Boss) && destroyedBy.owner == Owner.PLAYER)
            {
                enemiesKilled += 1;
                if(enemiesKilled / numberOfEnemies >= 0.75f)
                {
                    Achievement killed = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac1Name"), 1, GlobalGameValues.Values.GlobalStrings.getValue("ac1Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(killed))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(killed);
                    }
                }
            }
	    }

	    public void OnUpgrade(Actor sender, Weaponsystem upgraded)
	    {
            if (upgraded.level.value == 2)
            {
                weaponsystemsUpgraded += 1;
                Achievement fullUpgrade = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac4Name"), 4, GlobalGameValues.Values.GlobalStrings.getValue("ac4Dis"));
                if (!GlobalGameValues.Values.achievementsUnlocked.Contains(fullUpgrade))
                {
                    GlobalGameValues.Values.achievementsUnlocked.Add(fullUpgrade);
                }
                if (weaponsystemsUpgraded == 3)
                {
                    Achievement fullUpgradeAllWeapons = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac5Name"), 5, GlobalGameValues.Values.GlobalStrings.getValue("ac5Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(fullUpgradeAllWeapons))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(fullUpgradeAllWeapons);
                    }
                }
            }
	    }

	    public  void OnGameEnd(bool gameWon)
	    {
            if (gameWon)
            {
                if ((GlobalGameValues.Values.gameTime.TotalGameTime - start).TotalSeconds < 30)
                {
                    Achievement bossSpeed = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac2Name"), 2, GlobalGameValues.Values.GlobalStrings.getValue("ac2Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(bossSpeed))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(bossSpeed);
                    }
                }
                if (!damage)
                {
                    Achievement noDmg = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac0Name"), 0, GlobalGameValues.Values.GlobalStrings.getValue("ac0Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(noDmg))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(noDmg);
                    }
                }
                if (!damageBoss)
                {
                    Achievement noDmgBoss = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac3Name"), 3, GlobalGameValues.Values.GlobalStrings.getValue("ac3Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(noDmgBoss))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(noDmgBoss);
                    }
                }
                if (GlobalGameValues.Values.difficulty == Difficulty.DIFFICULTY_VERY_HARD)
                {
                    Achievement veryhard = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac7Name"), 7, GlobalGameValues.Values.GlobalStrings.getValue("ac7Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(veryhard))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(veryhard);
                    }
                }
            }
	    }

	    public void OnCollide(Actor sender, Actor actor2)
	    {
            if (sender is Aircraft && ((Aircraft)sender).controller is PlayerControlledAircraft)
            {
                damage = true;
                if (bossActive)
                {
                    damageBoss = true;
                }
            }
            if (sender is HealingBeam && actor2 is Aircraft && ((Aircraft)actor2).controller is PlayerControlledAircraft)
            {
                regeneratedHp += ((HealingBeam)sender).getDamage();
                if (regeneratedHp < -500)
                {
                    Achievement heal = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac6Name"), 6, GlobalGameValues.Values.GlobalStrings.getValue("ac6Dis"));
                    if (!GlobalGameValues.Values.achievementsUnlocked.Contains(heal))
                    {
                        GlobalGameValues.Values.achievementsUnlocked.Add(heal);
                    }
                }
            }
            if (sender is Aircraft && ((Aircraft)sender).controller is AIControlledAircraft_Drone && actor2 is Item)
            {
                AIControlledAircraft_Drone controller = (AIControlledAircraft_Drone)((Aircraft)sender).controller;
                if (controller.mode.value == DroneMode.COLLECT)
                {
                    collectedItems += 1;
                    if (collectedItems == 3)
                    {
                        Achievement collect = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac8Name"), 8, GlobalGameValues.Values.GlobalStrings.getValue("ac8Dis"));
                        if (!GlobalGameValues.Values.achievementsUnlocked.Contains(collect))
                        {
                            GlobalGameValues.Values.achievementsUnlocked.Add(collect);
                        }
                    }
                }
            }
	    }

        public void OnCreate(Actor sender)
        {
            if (sender is Aircraft && ((Aircraft)sender).controller is AIControlledAircraft_Boss)
            {
                start = GlobalGameValues.Values.gameTime.TotalGameTime;
                bossActive = true;
            }
        }

	    public AchievementHandler(int numberOfEnemies, string accountName)
	    {
            this.numberOfEnemies = numberOfEnemies;
            enemiesKilled = 0;
            start = TimeSpan.Zero;
            damage = false;
            damageBoss = false;
            bossActive = false;
            weaponsystemsUpgraded = 0;
            regeneratedHp = 0;
            collectedItems = 0;
            if (accountName.ToLower() == "chen")
            {
                Achievement Döner = new Achievement(GlobalGameValues.Values.GlobalStrings.getValue("ac9Name"), 9, GlobalGameValues.Values.GlobalStrings.getValue("ac9Dis"));
                if (!GlobalGameValues.Values.achievementsUnlocked.Contains(Döner))
                {
                    GlobalGameValues.Values.achievementsUnlocked.Add(Döner);
                }
            }
	    }

    }
}

